package cn.elricyu.tt.config;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import cn.elricyu.tt.bean.ActionBean;
import cn.elricyu.tt.bean.Server;
import cn.elricyu.tt.bean.Site;

public final class Config {
	private static final String prefix = "qq";

	private static DefaultConfig confIns = new DefaultConfig();

	public static final int SYSTEM_STATUS_ILLEGAL = 100;

	private static int illegal;

	private static Map<String, Integer> siteMap = null;

	private static List<ActionBean> actionBeanList = null;

	private static List<Server> serverList = new ArrayList<Server>();

	static {
		getServerList();
	}

	private static Set<String> userNameSet = Collections
			.synchronizedSet(new HashSet<String>());

	public static void addName(String name) {
		if (!containsName(name)) {
			userNameSet.add(name);
		}
	}

	public static boolean containsName(String name) {
		return userNameSet.contains(name);
	}

	public static boolean containsSite(String prefix) {
		if (siteMap == null) {
			synchronized (Config.class) {
				if (siteMap == null) {
					siteMap = new HashMap<String, Integer>();
					List<String> prefixList = confIns.getList("prefix");
					for (int i = 0; i < prefixList.size(); i++) {
						String p = prefixList.get(i);
						siteMap.put(p, i);
					}
				}
			}
		}
		return siteMap.containsKey(prefix);
	}

	public static long getActionSleepTime() {
		int min = 3000;
		int max = 1000 * confIns.getInt("action_sep_time", 5);
		if (max < min) {
			max = 10000;
		}
		long st = new Random().nextInt(max - min) + min;
		return st;
	}

	private static boolean getBoolean(String key, boolean defaultValue) {
		String v = confIns.getString(key);
		if (StringUtils.isBlank(v)) {
			return defaultValue;
		}
		return ("true".equalsIgnoreCase(v) || "1".equals(v));
	}

	public static Server getServer(String serverId) {
		for (Server server : getServerList()) {
			if (serverId.equals(server.getId())) {
				return server;
			}
		}
		return null;
	}

	public static List<Server> getServerList() {
		if (serverList.isEmpty()) {
			synchronized (Config.class) {
				if (serverList.isEmpty()) {
					List<String> urlList = confIns
							.getList("servers.server.url");
					List<String> idList = confIns.getList("servers.server.id");
					List<String> nameList = confIns
							.getList("servers.server.name");
					if (urlList != null && urlList.size() > 0) {
						for (int i = 0; i < urlList.size(); i++) {
							String url = urlList.get(i);
							String id = idList.get(i);
							String name = nameList.get(i);
							Server server = new Server();
							server.setId(id);
							server.setName(name);
							server.setUrl(url);
							serverList.add(server);
						}
					}
					List<String> remoteUrlList = RemoteConfig
							.getList("servers.server.url");
					List<String> remoteNameList = RemoteConfig
							.getList("servers.server.name");
					List<String> remoteIdList = RemoteConfig
							.getList("servers.server.id");
					if (remoteIdList != null && remoteIdList.size() > 0) {
						for (int i = 0; i < remoteIdList.size(); i++) {
							String url = remoteUrlList.get(i);
							String id = remoteIdList.get(i);
							String name = remoteNameList.get(i);
							Server server = new Server();
							server.setId(id);
							server.setName(name);
							server.setUrl(url);
							serverList.add(server);
						}
					}
				}
			}
		}
		return serverList;
	}

	public static List<ActionBean> getActionBeanList() {
		if (actionBeanList == null) {
			synchronized (Config.class) {
				if (actionBeanList == null) {
					actionBeanList = new ArrayList<ActionBean>();
					List<String> urlList = confIns
							.getList("actions.action.url");
					List<String> paramsList = confIns
							.getList("actions.action.params");
					List<String> descList = confIns
							.getList("actions.action.desc");
					if (urlList != null && urlList.size() > 0) {
						for (int i = 0; i < urlList.size(); i++) {
							String url = urlList.get(i);
							String params = paramsList.get(i);
							String desc = descList.get(i);
							ActionBean actionBean = new ActionBean(url, params,
									desc);
							actionBeanList.add(actionBean);
						}
					}
					List<String> remoteUrlList = RemoteConfig
							.getList("actions.action.url");
					List<String> remoteParamsList = RemoteConfig
							.getList("actions.action.params");
					List<String> remoteDescList = RemoteConfig
							.getList("actions.action.desc");
					if (remoteUrlList != null && remoteUrlList.size() > 0) {
						for (int i = 0; i < remoteUrlList.size(); i++) {
							String url = remoteUrlList.get(i);
							String params = remoteParamsList.get(i);
							String desc = remoteDescList.get(i);
							ActionBean actionBean = new ActionBean(url, params,
									desc);
							actionBeanList.add(actionBean);
						}
					}
				}
			}
		}
		return actionBeanList;
	}

	public static int getIllegal() {
		return illegal;
	}

	public static int getInsperctorTime() {
		return confIns.getInt("insperctor_time", 3600);
	}

	public static int getZoneInsperctorTime() {
		return confIns.getInt("zone_insperctor_time", 600);
	}

	public static int getMaxActionSleepTime() {
		return confIns.getInt("action_sep_time", 3);
	}

	public static String getProductADConfigUrl() {
		return getResourceUrl() + confIns.getString("infomation.config_name");
	}

	public static String getProductDisplay() {
		return getProductName() + " - v" + getVersion();
	}

	public static String getProductID() {
		return confIns.getString("infomation.productID");
	}

	public static String getProductName() {
		return confIns.getString("infomation.productName");
	}

	public static String getProductUrl() {
		return confIns.getString("infomation.url");
	}

	public static String getProductVersionADConfigUrl() {
		return getResourceUrl() + getVersion() + "/"
				+ confIns.getString("infomation.config_name");
	}

	public static String getQQLiveImageUrl() {
		return confIns.getString("qq.image_url");
	}

	public static String getQQTalkUrl() {
		return confIns.getString("qq.talk_url");
	}

	public static String getWWLiveImageUrl() {
		return confIns.getString("ww.image_url");
	}

	public static String getWWTalkUrl() {
		return confIns.getString("ww.talk_url");
	}

	public static String getRegBindUrl() {
		return confIns.getString("reg.bind_url");
	}

	public static List<String> getRegHomeList() {
		return confIns.getList("reg.home");
	}

	public static String getResourceUrl() {
		return confIns.getString("infomation.resource_url") + getProductID()
				+ "/";
	}

	public static Site getSite() {
		Site site = new Site();

		String inputUrl = confIns.getString(prefix + ".login_input_url");
		String submitUrl = confIns.getString(prefix + ".login_submit_url");
		String loginCheckUrl = confIns.getString(prefix + ".login_check_url");
		String loginCheckImageUrl = confIns.getString(prefix
				+ ".login_check_image_url");
		String checkFollowUrl = confIns.getString(prefix + ".check_follow_url");
		site.setLoginInputUrl(inputUrl);
		site.setLoginSubmitUrl(submitUrl);
		site.setLoginCheckUrl(loginCheckUrl);
		site.setLoginCheckImageUrl(loginCheckImageUrl);
		site.setCheckFollowUrl(checkFollowUrl);
		return site;
	}

	public static String getPengyouIndexUrl(String entry) {
		return confIns.getString(prefix + "." + entry + ".index_url");
	}

	public static String getAppCallbackUrl(String entry) {
		return confIns.getString(prefix + "." + entry + ".app_callback_url");
	}

	public static String getUgsUrl(String entry) {
		return confIns.getString(prefix + "." + entry + ".ugs_url");
	}

	public static String getApiUrl(String entry) {
		return confIns.getString(prefix + "." + entry + ".api_url");
	}

	public static int getThreadCount() {
		return confIns.getInt("thread_count", 5);
	}

	public static String getVersion() {
		return confIns.getString("infomation.version.major") + "."
				+ confIns.getString("infomation.version.minor")
				+ confIns.getString("infomation.version.revision");
	}

	public static String getDBVersion() {
		return confIns.getString("infomation.dbversion");
	}

	public static String getDBFileName() {
		return getProductID() + "-" + getDBVersion();
	}

	public static boolean isAutoRun() {
		return getBoolean("auto_run", true);
	}

	public static boolean isSpecialConsume() {
		return getBoolean("special_consume", false);
	}

	public static boolean isSpecialInviteFriends() {
		return getBoolean("special_invite_friend", false);
	}

	public static boolean isSpecialOpenBox() {
		return getBoolean("special_open_box", false);
	}

	public static boolean isSpecialSearchFriends() {
		return getBoolean("special_search_friend", false);
	}

	public static boolean isSpecialDebug() {
		return getBoolean("special_debug", false);
	}

	public static void setIllegal(int i) {
		illegal = i;
	}

	public static String getRemoteConfigUrl() {
		return getResourceUrl() + confIns.getString("infomation.settings_name");
	}

	public static String getVersionRemoteConfigUrl() {
		return getResourceUrl() + getVersion() + "/"
				+ confIns.getString("infomation.settings_name");
	}

	public static int getEquipProValue(String key) {
		return confIns.getInt("equip_pro." + key + ".value", 1);
	}

	public static String getEquipProName(String key) {
		return confIns.getString("equip_pro." + key + ".name", key);
	}

	public static boolean isProductMode() {
		return getBoolean("product_mode", true);
	}

	public static String getPubKey() {
		return confIns.getString("pub_key", "");
	}

	public static String getSignText() {
		return confIns.getString("sign_text", "");
	}
}
